import { http } from "@/utils/fetch";

/**
 * 上传单个文件
 * @param formData 表单数据
 * @returns 返回结果
 */
// export const uploadFile = async (formData: FormData) => {
//   return await http("POST", "/file/upload", formData, undefined, {});
// };

/**
 * 上传多个文件
 * @param formData 表单数据
 * @returns 返回结果
 */
export const uploadFileMany = async (formData: FormData, remark = "") => {
  return await http("POST", `/media/upload?remark=${remark}`, formData, undefined, {});
};

/**
 * 通过鉴权的方式流式下载文件
 * @param fileId 附件 ID
 * @returns 下载文件
 */
export const downloadFileStream = async (fileId: string) => {
  const fileData = await http("GET", `/media/findOne?id=${fileId}`);

  const fileBlob = await http("GET", `/media/download?id=${fileId}`, null, "blob");

  const fileName = fileData.data.name;

  // 创建一个下载链接，并点击链接触发下载
  const url = window.URL.createObjectURL(fileBlob);
  const a = document.createElement("a");
  a.href = url;
  a.download = fileName;
  document.body.appendChild(a);
  a.click();
  window.URL.revokeObjectURL(url);
  document.body.removeChild(a);
};

export const deleteFile = async (id: string) => {
  return await http("DELETE", `/media/delete?id=${id}`);
};

export const deleteFileMany = async (ids: { deleteIds: string[] }) => {
  return await http("POST", "/media/batchDelete", ids);
};
